package br.uefs.cedoc.db.processo;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import br.uefs.cedoc.db.DBController;
import br.uefs.cedoc.db.exception.DBConnectionException;
import br.uefs.cedoc.entidades.ProcessoBean;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;


/**
 *
 * @author suporte
 */
public class DBProcesso {

    public void criarProcesso(ProcessoBean processo) throws SQLException, DBConnectionException {

        String comandoSQL = "insert into processo (idprocesso,secao_idsecao," +
                "comarca_idcomarca,tipologia_idtipologia,fundo_idfundo," +
                "localidade_idlocalidade,serie_idserie,subserie_idsubserie," +
                "caixa_idcaixa,periodoInicial,periodoFinal,folhas) values " +
                "(?,?,?,?,?,?,?,?,?,?,?,?)";

        Connection conn = DBController.getInstance().getConnection();

        if (conn != null) {

            System.out.println(""+processo.getFundo());
            PreparedStatement prepStatement = conn.prepareStatement(comandoSQL);
            prepStatement.setInt(1, processo.getIdDocumento());
            prepStatement.setInt(2, processo.getSecao());
            prepStatement.setInt(3, processo.getComarca());
            prepStatement.setInt(4, processo.getTipologia());
            prepStatement.setInt(5, processo.getFundo());
            prepStatement.setInt(6, processo.getLocalidade());
            prepStatement.setInt(7, processo.getSerie());
            prepStatement.setInt(8, processo.getSubserie());
            prepStatement.setInt(9, processo.getCaixa());
            prepStatement.setInt(10, processo.getPeriodoInicial());
            prepStatement.setInt(11, processo.getPeriodoFinal());
            prepStatement.setInt(12, processo.getFolhas());
            //prepStatement.setString(13, processo.getObservacao());
            //DBOperacoes dbo = new DBOperacoes();
            //processo.setIdProcesso(dbo.getMaxID("idprocesso", "processo"));	
            //System.out.println("IDSECAO= "+processo.getIdprocesso());
            //prepStatement.setInt(1, processo.getIdprocesso());
            prepStatement.execute();
		
            prepStatement.close();
        } else {
            DBConnectionException dbException = new DBConnectionException(" Erro de conexao com banco ");
            dbException.printStackTrace();
            throw dbException;
        }




    }

    public synchronized void editarProcesso(ProcessoBean processo) throws SQLException, DBConnectionException {

        String comandoSQL = "UPDATE processo SET processo = ? WHERE idprocesso = ?";

        Connection conn = DBController.getInstance().getConnection();
        //System.out.println("Editar acervo: "+acervo.getNome());
        if (conn != null) {

//            PreparedStatement prepStatement = conn.prepareStatement(comandoSQL);
//            prepStatement.setString(1, processo.getProcesso());
//            prepStatement.setInt(2, processo.getIdProcesso());
//            prepStatement.execute();
//            prepStatement.close();

        } else {
            DBConnectionException dbException = new DBConnectionException(" Erro de conex�o com banco ");
            dbException.printStackTrace();
            throw dbException;
        }


    }

    public ProcessoBean listaProcesso(int id) throws SQLException, DBConnectionException {
        String comandoSQL = "SELECT * FROM processo WHERE idprocesso=?"; // incluir
        // limites

        Connection conn = DBController.getInstance().getConnection();
        try {
            if (conn != null) {
                PreparedStatement prepStatement = conn.prepareStatement(comandoSQL);

                prepStatement.setInt(1,id);
                ResultSet rs = prepStatement.executeQuery();



                while (rs.next()) {
                    ProcessoBean processo = new ProcessoBean();
//
//                    processo.setIdProcesso(rs.getInt("idprocesso"));
//                    processo.setProcesso(rs.getString("processo"));
                    
                    return processo;

                }
                DBController.getInstance().closeAll(conn, null, null);
               


            } else {
                DBConnectionException dbException = new DBConnectionException(" Erro de conex�o com banco ");
                dbException.printStackTrace();
                throw dbException;
            }
            return null;
        } catch (NullPointerException ex) {
            ex.printStackTrace();
            return null;
        }
    }



    	public  void removerProcesso(int id) throws SQLException {

		String comandoSQL = "DELETE FROM processo WHERE idprocesso=?";

		Connection conn = DBController.getInstance().getConnection();
		
		if (conn != null){
			

			PreparedStatement prepStatement = conn.prepareStatement(comandoSQL);
			prepStatement.setInt(1, id);						
			prepStatement.execute();			
			prepStatement.close();
		}	

	}

    public ProcessoBean[] listarProcessos() throws SQLException, DBConnectionException {
        String comandoSQL = "SELECT * FROM processo "; // incluir
        // limites

        Connection conn = DBController.getInstance().getConnection();
        try {
            if (conn != null) {
                PreparedStatement prepStatement = conn.prepareStatement(comandoSQL);


                ResultSet rs = prepStatement.executeQuery();

                Collection processos = new ArrayList();

                while (rs.next()) {
                    ProcessoBean processo_temp = new ProcessoBean();

//                    processo_temp.setIdProcesso(rs.getInt("idprocesso"));
//                    processo_temp.setProcesso(rs.getString("processo"));
                    processos.add(processo_temp);

                }
                DBController.getInstance().closeAll(conn, null, null);
                return (ProcessoBean[]) processos.toArray(new ProcessoBean[0]);


            } else {
                DBConnectionException dbException = new DBConnectionException(" Erro de conex�o com banco ");
                dbException.printStackTrace();
                throw dbException;
            }
        } catch (NullPointerException ex) {
            ex.printStackTrace();
            return null;
        }
    }
}

